

//
//  visitorView.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/6.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit

class visitorView: UIView {
    

    /// 设置信息
    ///
    /// - parameter imageName: 图片
    /// - parameter title:     信息
    func setUpInfo(imagName imageName:String?,title:String){
        
        setUpUI()
        
        titleLable.text = title
//
        guard let imgName = imageName else{
            startAnimation()
            return
        }

        houseView.image = UIImage(named: imgName)
        //隐藏house
        iconView.hidden = true
    }
    /// 动画
    func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    //MARK: - 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    //        fatalError("init(coder:) has not been implemented")
        setUpUI()
    }
    //创建
    
    //圈
    private lazy var iconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")

    /// 房子
    private lazy var houseView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")

    /// 信息
    private lazy var titleLable:UILabel = UILabel(txt: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")

    /// 注册按钮
    lazy var registerbtn:UIButton = UIButton(titile: "注册", backGroundImage: "common_button_white_disable",txtColor: UIColor.orangeColor())

    /// 登陆按钮
    lazy var loginBtn:UIButton = UIButton(titile: "登陆", backGroundImage: "common_button_white_disable")

    ///遮罩
    private lazy var shadowView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    /// 设置信息
   private func setUpUI(){
        //添加到视图上
        addSubview(iconView)
        addSubview(shadowView)
        addSubview(houseView)
        addSubview(titleLable)
        addSubview(registerbtn)
        addSubview(loginBtn)
    
        //设置约束
        //取消autoresizing
        for subview in subviews{
            subview.translatesAutoresizingMaskIntoConstraints = false
        }

        //        NSLayoutConstraint
        //iconView
        addConstraint(NSLayoutConstraint.init(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //houseView
        addConstraint(NSLayoutConstraint.init(item: houseView, attribute:NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //Lable
        addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 60))
        
        //registerbtn
        addConstraint(NSLayoutConstraint(item: registerbtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerbtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: -10))
        addConstraint(NSLayoutConstraint(item: registerbtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: UIScreen.mainScreen().bounds.size.width * 0.5 - 100))
        addConstraint(NSLayoutConstraint(item: registerbtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        
        //loginBtn
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: UIScreen.mainScreen().bounds.size.width * 0.5 - 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
    //
//    let h = loginBtn.bounds.size.heightn VisualFormatLimit
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[shadow]-0-|", options: [], metrics: nil, views: ["shadow":shadowView]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[shadow]-(H)-[loginBtn]", options: [], metrics: ["H":-30], views: ["shadow":shadowView,"loginBtn":loginBtn]))
    backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    /// 环形图案
     func setUpCircleView () {
        
        //创建
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        //添加到视图上
        addSubview(iconView)
        //设置约束
        //取消autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        //        NSLayoutConstraint
        addConstraint(NSLayoutConstraint.init(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
    }

}
